﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace FontTool
{
    public class commandManager
    {
        private List<command> commandStack = new List<command>();
        int index = -1;
        int stackCount = 0;
        bool refresh = false;

        public void executeCommand(nFont font, command cmd)
        {
            refreshRequired();

            cmd.Execute(font);
            commandStack.Add(cmd);

            if (refresh)
            {
                commandStack.Clear();
                stackCount = 0;
                index = -1;
                refresh = false;
            }
            stackCount++;
            index++;
        }

        public void Undo(nFont font)
        {
            if (index >= 0)
            {
                command cmd = commandStack[index];
                cmd.Undo(font);

                if (index > 0)
                    index--;
            }
        }

        public void Redo(nFont font)
        {
            if (index < commandStack.Count)
            {
                command cmd = commandStack[index];
                cmd.Redo(font);

                if(index < commandStack.Count-1)
                index++;
            }
        }

        void refreshRequired()
        {
            if (index + 1 != stackCount)
                refresh = true;
        }
    }
}
